package com.lz.dubbo.consumer.common.utils;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * @author: zip.li
 * @date: Created in 2022/9/30 10:51
 * @Description:
 */
public class ParseUtils {

    public static BigDecimal converToBigDecimal(Object source) {
        BigDecimal target = new BigDecimal(BigInteger.ZERO);
        if (source instanceof Integer) {
            target = BigDecimal.valueOf((int) source).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        if (source instanceof Double) {
            target = new BigDecimal((Double) source).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        if (source instanceof Float) {
            target = new BigDecimal(Float.toString((Float) source)).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        if (source instanceof BigDecimal) {
            target = ((BigDecimal) source).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        if (source instanceof String) {
            if (source.equals("") && ((String) source).length() == 0) {

            } else {
                target = new BigDecimal((String) source).setScale(2, BigDecimal.ROUND_HALF_UP);
            }
        }
        return target;
    }

    public static Double converToDouble(Object source) {
        Double target = new Double(0);
        if (source instanceof String) {
            if (source.equals("") && ((String) source).length() == 0) {

            } else {
                target = Double.parseDouble((String) source );
            }
        }else if (source instanceof BigDecimal) {
            target =  ((BigDecimal) source).doubleValue();
        }else if (source instanceof Integer) {
            target = ((Integer) source).doubleValue();
        }else if (source instanceof Float) {
            target = ((Float) source).doubleValue();
        } else if (source instanceof Double) {
            target = (Double) source;
        }
        return target;
    }

}
